package com.stone.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stone.bean.AppResponse;
import com.stone.bean.dto.ConfigAddDTO;
import com.stone.bean.dto.ConfigUpdateDTO;
import com.stone.constant.Const;
import com.stone.entity.SysConfig;
import com.stone.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/sysConfig")
@Api(tags = "字典管理")
public class SysConfigController {

    @Resource
    private SysConfigService sysConfigService;

    @GetMapping("/query")
    @ApiOperation("根据父级查询子级")
    public AppResponse<List<SysConfig>> query(@RequestParam(value = "parentCode", required = false) String parentCode) {
        List<SysConfig> list = sysConfigService.list(new QueryWrapper<SysConfig>().eq("parent_code", parentCode));
        return AppResponse.success().setRecords(list);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation("新增字典")
    public AppResponse add(@RequestBody @Valid ConfigAddDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        sysConfigService.add(dto, token);
        return AppResponse.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("修改字典")
    public AppResponse update(@RequestBody @Valid ConfigUpdateDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        sysConfigService.update(dto, token);
        return AppResponse.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除字典")
    public AppResponse delete(@PathVariable(value = "id") Long id) {
        sysConfigService.delete(id);
        return AppResponse.success();
    }

    /**
     * 详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation("字典详情")
    public AppResponse<SysConfig> getById(@PathVariable(value = "id") Long id) {
        SysConfig sysConfig = sysConfigService.getById(id);
        return AppResponse.success().setRecord(sysConfig);
    }

}
